<?php

namespace App\Admin\Controllers;

use App\Models\Navigation;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;

class NavigationController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '网站导航栏';

    public function index(Content $content)
    {
        return $content
            ->title(trans('admin.menu'))
            ->description(trans('admin.list'))
            ->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(route('navigations.store'));
                    $form->select('parent_id', trans('admin.parent_id'))->options(Navigation::selectOptions());
                    $form->text('title', trans('table.navigation.title'))->rules('required');
                    $form->text('url', trans('table.navigation.url'));
                    $form->radio('status',trans('table.navigation.status'))->options(['1' => 'enable', '0'=> 'disable'])->default('1');
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
    }

    /**
     * @return \Encore\Admin\Tree
     */
    protected function treeView()
    {
        $menuModel = Navigation::class;

        $tree = new Tree(new $menuModel());

        $tree->disableCreate();

        $tree->branch(function ($branch) {
            $payload = "<strong>{$branch['title']}</strong>";
            if (!isset($branch['children'])) {
                if (url()->isValidUrl($branch['url'])) {
                    $uri = $branch['url'];
                } else {
                    $uri = route($branch['url']);
                }

                $payload .= "&nbsp;&nbsp;&nbsp;<a href=\"$uri\" class=\"dd-nodrag\">$uri</a>";
            }

            return $payload;
        });

        return $tree;
    }



    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Navigation::findOrFail($id));

        $show->field('id', __('table.navigation.id'));
        $show->field('title', __('table.navigation.title'));
        $show->field('url', __('table.navigation.Url'));
        $show->field('status', __('table.navigation.status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Navigation());

        $form->text('title', __('table.navigation.title'));
        $form->text('url', __('table.navigation.url'));
        $form->select('parent_id', __('table.navigation.parent_id'))->options(Navigation::selectOptions());
        $form->number('order', __('table.navigation.order'));
        $form->switch('status', __('table.navigation.status'))->default(1);
        return $form;
    }

}
